package com.james.happyfarm.servlets;

import java.io.IOException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.urlfetch.HTTPHeader;
import com.google.appengine.api.urlfetch.HTTPMethod;
import com.google.appengine.api.urlfetch.HTTPRequest;
import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;

public class TestServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String urlStr = "http://wap.kaixin001.com/login/login.php";
		String data = URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode("tanhongxu@126.com", "UTF-8")
			+ "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode("75158579", "UTF-8");
		
		HTTPRequest kaixinReq = new HTTPRequest(new URL(urlStr), HTTPMethod.POST);
		kaixinReq.setPayload(data.getBytes());
		URLFetchService ufs = URLFetchServiceFactory.getURLFetchService();
		HTTPResponse kaixinResp = ufs.fetch(kaixinReq);

		List<HTTPHeader> headerList = kaixinResp.getHeaders();
		
		StringBuffer sb = new StringBuffer();

		for (HTTPHeader header : headerList) {
			sb.append(header.getName() + " : " + header.getValue() + "<br />");
		}
		
		resp.getWriter().write(sb.toString());
	}

	
}
